package nuonuo.com.view.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.TextView;

import nuonuo.com.view.DimenUtils;

/**
 * Created by n6 on 2017/3/3.
 */

public class QQDeleteLinearLayout extends LinearLayout {
    private Scroller mScroller;
    private int mLastInterceptedX, mLastInterceptedY;
    private int mLastX, mLastY;
    private VelocityTracker mVelocityTracker;
    private static int STATE;
    private static int SHOW_DEL = 1;
    private static int INIT_STATE = 2;
    private static int mContentWidth;
    private static int mMaxLength;
    private boolean flag = false;
    private View mDel;


    public QQDeleteLinearLayout(Context context) {
        this(context, null);
    }

    public QQDeleteLinearLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QQDeleteLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mScroller = new Scroller(context);
        mVelocityTracker = VelocityTracker.obtain();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!flag){
            flag = true;
            mMaxLength = MeasureSpec.getSize(widthMeasureSpec);
        }
        int childCount = getChildCount();
        int mHeight = 0;
        for (int i = 0; i < childCount; i++) {
            View mCurView = getChildAt(i);
            LayoutParams lp = (LayoutParams) mCurView.getLayoutParams();
            int bottomMargin = lp.bottomMargin;
            int topMargin = lp.topMargin;
            if (i != childCount - 1) {
                measureChild(mCurView, MeasureSpec.makeMeasureSpec(mMaxLength,MeasureSpec.EXACTLY), heightMeasureSpec);
                mHeight = Math.max(mHeight,mCurView.getMeasuredHeight() + bottomMargin + topMargin);
                if (mHeight != mCurView.getMeasuredHeight() + bottomMargin + topMargin){
                    if (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT)
                        mCurView.measure(MeasureSpec.makeMeasureSpec(mCurView.getMeasuredWidth(),MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(mHeight,MeasureSpec.EXACTLY));
                }
                Logger.i(mHeight+"");
            } else {
                Logger.i(mHeight+"==========");
                int mDelWidthSpec = MeasureSpec.makeMeasureSpec(mMaxLength / 4, MeasureSpec.EXACTLY);
                measureChild(mCurView, mDelWidthSpec, heightMeasureSpec);
                if (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT)
                    mCurView.measure(MeasureSpec.makeMeasureSpec(mCurView.getMeasuredWidth(),MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(mHeight,MeasureSpec.EXACTLY));
                mContentWidth = mMaxLength / 4;
            }
        }
        setMeasuredDimension(mMaxLength + mContentWidth, mHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int childWidth;
        int childHeight;
        int mCurWidth = 0;
        for (int i = 0; i < childCount; i++) {
            View mCurView = getChildAt(i);
            LayoutParams lp = (LayoutParams) mCurView.getLayoutParams();
            int bottomMargin = lp.bottomMargin;
            int topMargin = lp.topMargin;
            int leftMargin = lp.leftMargin;
            int rightMargin = lp.rightMargin;
            childHeight = mCurView.getMeasuredHeight();
            childWidth = mCurView.getMeasuredWidth();
            if (i != childCount - 1){
                mCurView.layout(leftMargin + mCurWidth, topMargin, leftMargin + mCurWidth + childWidth, childHeight + topMargin);
                mCurWidth = mCurWidth + leftMargin + rightMargin + childWidth;
            }else {
                int measuredWidth = getMeasuredWidth();
                mCurView.layout(measuredWidth - mContentWidth, topMargin, measuredWidth, childHeight + topMargin);
                Logger.i(mCurView.getTop()+"___"+mCurView.getLeft()+"___"+mCurView.getBottom()+"___"+mCurView.getRight());
            }
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean intercepted = false;
        int x = (int) ev.getX();
        int y = (int) ev.getY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i("mtag",getMeasuredWidth()+"");
                if (!mScroller.isFinished()) {
                    intercepted = true;
                    mScroller.abortAnimation();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaX = x - mLastInterceptedX;
                int deltaY = y - mLastInterceptedY;
                if (Math.abs(deltaX) > Math.abs(deltaY)) {
                    intercepted = true;
                }
                break;
            case MotionEvent.ACTION_UP:
                intercepted = false;
                break;
            default:
                break;
        }
        mLastInterceptedX = x;
        mLastInterceptedY = y;
        return intercepted;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mDel == null){
            mDel = getChildAt(getChildCount() - 1);
        }
        mVelocityTracker.addMovement(event);
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (!mScroller.isFinished()) {
                    mScroller.abortAnimation();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaX = x - mLastX;
                scrollBy(-deltaX, 0);
                break;
            case MotionEvent.ACTION_UP:
                int mScrollX = getScrollX();
                mVelocityTracker.computeCurrentVelocity(1000);
                float xVelocity = mVelocityTracker.getXVelocity();
                if (xVelocity >= 50) {
                    smoothScrollTo(mDel.getWidth(), 0);
                } else {
                    if ((mScrollX + mDel.getWidth() / 2) / mDel.getWidth() >= 1) {
                        smoothScrollTo(mDel.getWidth(), 0);
                    } else {
                        smoothScrollTo(0, 0);
                    }
                }
                break;
            default:
                break;
        }
        mLastX = x;
        mLastY = y;
        return true;
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }

    private void smoothScrollTo(int destX, int destY) {
        int startX = getScrollX();
        int deltaX = destX - startX;
        mScroller.startScroll(startX, 0, deltaX, 0, 500);
        invalidate();
    }
}
